package Network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    private Socket clientSocket=null;

    public TcpEchoClient(String serviceIP,int servicePort) throws IOException {
        clientSocket=new Socket(serviceIP,servicePort);
    }
    public void start()  {
        System.out.println("客户端启动");
        try(InputStream inputStream=clientSocket.getInputStream();
            OutputStream outputStream=clientSocket.getOutputStream();
            Scanner scanNetwork=new Scanner(inputStream);
            Scanner scanConsole=new Scanner(System.in);
            PrintWriter printWriter=new PrintWriter(outputStream)){

            while(true){
                System.out.println("->");
                if(!scanConsole.hasNext()){
                    break;//客户端输入结束
                }
                //从控制台 获取用户输入请求
                String request=scanConsole.next();
                //将请求发给服务端 通过println发送
                printWriter.println(request);
                printWriter.flush();//通过flush 刷新确保数据被发送出去
                //从服务端读取响应
                String response=scanNetwork.next();
                //打印输出
                System.out.println(response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                clientSocket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient = new TcpEchoClient("127.0.0.1", 9090);
        tcpEchoClient.start();
    }
}

class TcpEchoClient1{
    //1.创建Socket对象
    private Socket clientSocket=null;
    //传入服务端的IP和端口号
    public TcpEchoClient1(String serverIp,int serverPort) throws IOException {
        clientSocket=new Socket(serverIp,serverPort);
    }
    public void start() throws IOException {
        System.out.println("客户端启动");
        try(InputStream inputStream=clientSocket.getInputStream();
            OutputStream outputStream=clientSocket.getOutputStream();
            Scanner scanNetWork=new Scanner(inputStream);
            Scanner scanConsole=new Scanner(System.in);
            PrintWriter printWriter=new PrintWriter(outputStream) ) {

            while(true){
                System.out.print("->");
                if(!scanConsole.hasNext()){
                    break;//客户端请求输入结束
                }
                //1.获取请求
                String request=scanConsole.next();
                //2.将请求发送给服务端 通过PrintWriter的println方法发送
                printWriter.println(request);
                printWriter.flush();
                //3.获取响应
                String response=scanNetWork.next();
                //4.输出响应
                System.out.println(response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            clientSocket.close();
        }
    }
    public static void main(String[] args) throws IOException {
        TcpEchoClient1 tcpEchoClient1 = new TcpEchoClient1("127.0.0.1", 9090);
        tcpEchoClient1.start();
    }
}